package com.sczmgk.hotline_pzh.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.lidroid.xutils.BitmapUtils;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.entity.AppealPicBean;
import com.sczmgk.hotline_pzh.utils.MyLog;
import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.IndicatorViewPager;

import java.util.List;

import uk.co.senab.photoview.PhotoView;

public class AppealLargePicActivity extends Activity {

    private static final String TAG = AppealLargePicActivity.class.getSimpleName();
    private IndicatorViewPager indicatorViewPager;
    private LayoutInflater inflate;
    private BitmapUtils bitmapUtils;
    private int position;
    /**
     * 用户申诉图片的集合
     */
    private List<AppealPicBean> appealPics;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        setContentView(R.layout.activity_appeal_large_pic);
        intiData();
        bitmapUtils = new BitmapUtils(this);
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.ARGB_4444);
        bitmapUtils.configDefaultLoadFailedImage(getResources().getDrawable(R.drawable.default_center));
        bitmapUtils.configDefaultLoadingImage(getResources().getDrawable(R.drawable.ic_logo));


        //ViewPager,Indicator
        ViewPager viewPager = (ViewPager) findViewById(R.id.guide_viewPager);
        viewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyLog.d(TAG,"viewPager   click!");
                finish();
            }
        });
        Indicator indicator = (Indicator) findViewById(R.id.guide_indicator);
        indicatorViewPager = new IndicatorViewPager(indicator, viewPager);

        inflate = LayoutInflater.from(getApplicationContext());
        indicatorViewPager.setAdapter(adapter);
        indicatorViewPager.setCurrentItem(position,false);

    }

    private void intiData() {
        Intent intent = getIntent();
        appealPics = (List<AppealPicBean>) intent.getSerializableExtra("picUrls");
        position = intent.getIntExtra("position",0);
    }


    private IndicatorViewPager.IndicatorPagerAdapter adapter = new IndicatorViewPager.IndicatorViewPagerAdapter() {

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = inflate.inflate(R.layout.tab_guide, container, false);
            }

            return convertView;
        }

        @Override
        public View getViewForPage(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = inflate.inflate(R.layout.appeal_pic_large_item,null);
            }
            PhotoView photoView = (PhotoView) convertView.findViewById(R.id.pv_appeal_large_pic);
            RelativeLayout.LayoutParams barLayoutParam = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            photoView.setLayoutParams(barLayoutParam);
            bitmapUtils.display(photoView, appealPics.get(position).getImageUri());
            //必须这样调用点击事件
            photoView.onClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
           return  convertView;
        }

        @Override
        public int getItemPosition(Object object) {
            //这是ViewPager适配器的特点,有两个值 POSITION_NONE，POSITION_UNCHANGED，默认就是POSITION_UNCHANGED,
            // 表示数据没变化不用更新.notifyDataChange的时候重新调用getViewForPage
            return PagerAdapter.POSITION_NONE;
        }

        @Override
        public int getCount() {
            return appealPics.size();
        }
    };
}
